const timeout = {
	timeout(e) {
		var key_time = uni.getStorageSync('key_time');
		var login = uni.getStorageSync('login');
		var expires_in = uni.getStorageSync('expires_in');
		if (login) {
			if (Date.parse(new Date()) - key_time > (Number(expires_in) - 600) * 1000) {
				
				var auth = 'weixing-mall-mem:mem-client-security';
				// 对字符串进行编码
				var encode = encodeURI(auth);
				// 对编码的字符串转化base64
				var base64 = btoa(encode);
				var refresh_token = uni.getStorageSync('refreshToken');
				console.log(refresh_token);
				uni.request({
					// #ifndef H5
					   url: 'http://lm.weixingshishang.cn/api/uaa/token/refresh', //仅为示例，并非真实接口地址。
					 // url: 'https://1511w8t629.51mypc.cn/uaa/token/refresh',
					// #endif
					// #ifdef H5
					 url: '/api/uaa/token/refresh', //仅为示例，并非真实接口地址。
					 // #endif
					data: {
						refreshToken: refresh_token
					},
					method: 'Post',
					header: {
						'Authorization': 'Basic ' + base64,
						'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
					},
					success: res => {
						console.log(res)
						if (res.data.code == 200) {
							uni.setStorageSync('Authorization', 'Bearer ' + res.data.data.access_token);
							uni.setStorageSync('refreshToken', res.data.data.refresh_token);
							uni.setStorageSync('expires_in', res.data.data.expires_in);
							uni.setStorageSync('key_time', res.data.data.timestamp);
							return true
						} else if (res.data.code == 401) {
							uni.removeStorageSync('Authorization')
							uni.removeStorageSync('refreshToken')
							uni.removeStorageSync('expires_in')
							uni.removeStorageSync('key_time')
							uni.removeStorageSync('login')
							uni.showModal({
							    title: '提示',
							    content: '登录已失效，是否重新登录',
							    success: function (res) {
							        if (res.confirm) {
							            uni.navigateTo({
							            	url: '/pages/shop/account/login'
							            });
							        } else if (res.cancel) {
							            console.log('用户点击取消');
							        }
							    }
							});
							return false
						} else {
							// uni.showToast({
							// 	title: '请检查网络后重试',
							// 	icon: "none",
							// 	duration: 2000
							// });
							return false
						}
					}
				});
		
				// #ifndef H5
				// #endif
				// #ifdef H5
				// uni.removeStorageSync('key_time');
				// uni.removeStorageSync('login');
				// uni.removeStorageSync('expires_in');
				// uni.removeStorageSync('refresh_token');
				// uni.removeStorageSync('Authorization');
				// uni.removeStorageSync('mr');
				// uni.removeStorageSync('ur');
				// if (e !== 'no') {
				// 	uni.navigateTo({
				// 		url: '/pages/shop/account/login'
				// 	});
				// }
				// return false
				// #endif
			} else {
				return true
			}
		} else {
			return false
		}
	}
}
export default timeout
